package DAO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import DBUntil.DbHelper;

public class UserDAO {
	private static DbHelper db = new DbHelper();

	public static String getlinkNo() {
		String linkNo = "";
		// 用字符数组的方式随机
		String model = "0123456789";
		char[] m = model.toCharArray();
		for (int j = 0; j < 6; j++) {
			char c = m[(int) (Math.random() * 10)];
			// 保证六位随机数之间没有重复的
			linkNo = linkNo + c;
		}
		return linkNo;
	}

	public static int UserAdd(String name, String pwd, byte[] image) throws Exception {

		String uid = null;
		String sql = "insert into user_account values(?,?,?,?,?,?)";

		String sql1 = "select user_id,uname,upassword from user_account where user_id = ?";
		while (true) {
			uid = getlinkNo();
			Map<String, Object> map = db.findsingle(sql1, uid);
			if (null == map) {
				break;
			}
		}
		return db.update(sql, uid, name, pwd, "C:\\QQMusic\\" + uid + "\\caching", "C:\\QQMusic\\" + uid + "\\dow",
				image);
	}

	// 用户登录
	public static Map<String, Object> login(String id, String pwd) throws IOException {
		String sql = "select user_id,uname,upassword,user_cachingpath,user_dowpath,user_image from user_account where user_id=? and upassword=?";
		return db.findsingle(sql, id, pwd);
	}

	// 用户登录
	public static Map<String, Object> finduser(String name, String pwd) throws IOException {
		String sql = "select user_id,uname,upassword,user_cachingpath,user_dowpath,user_image from user_account where uname=? and upassword=?";
		return db.findsingle(sql, name, pwd);
	}

	// 修改密码
	public static int updatePwd(String user_id, String upassword) throws Exception {
		String sql = "update user_account set upassword =? where user_id =?";
		return db.update(sql, upassword, user_id);
	}

	// 修改昵称
	public static int updateName(String user_id, String name) throws Exception {
		String sql = "update user_account set uname =? where user_id =?";
		return db.update(sql, name, user_id);
	}

	// 修改头像
	public static int updateImage(String user_id, byte[] user_image) throws Exception {
	
		String sql = "update user_account set user_image =? where user_id =?";
		return db.update(sql, user_image, user_id);
	}

}
